﻿#include "EditorAssetSelectorWidget.h"

#include "Framework/Editor.h"
#include "Theme/EditorTheme.h"

#include "Widgets/Keyboard/KeyboardWidget.h"
#include "Widgets/Mouse/MouseWidget.h"
#include "Widgets/RectangleWidget.h"
#include "Widgets/TextWidget.h"
#include "Widgets/ImageWidget.h"

#include "Project/ProjectModel.h"
#include "Project/ProjectViewModel.h"
#include "Asset/ProjectAssetSubsystem.h"

#include "Window/EditorPopupMenuView.h"
#include "Window/EditorWindowGroupView.h"

#include "Texture/Texture2D.h"
#include "Material/Material.h"
#include "Mesh/StaticMesh.h"

REditorAssetSelectorWidget::REditorAssetSelectorWidget()
{
    
    
}

void REditorAssetSelectorWidget::SetAssetType(EEditorSelectableAssetType InAssetType)
{    
    auto Background = NewWidget<RRectangleWidget>(this);
    Background->Anchor.Fill(this);
    Background->FillColor= REditor::Get()->CurrentTheme->BackgroundColor;
    Background->Radius=4;
    Background->BorderSize=1;
    Background->BorderColor= REditor::Get()->CurrentTheme->SpliterColor;


    const float Padding=4;
    auto List=NewWidget<RWidget>(Background);
    List->Anchor.LeftReference=Background;
    List->Anchor.LeftOffset=Padding;
    List->Anchor.RightReference=Background;
    List->Anchor.RightOffset=Padding;
    List->Anchor.TopReference=Background;
    List->Anchor.TopOffset=Padding;
    List->Anchor.BottomReference=Background;
    List->Anchor.BottomOffset=Padding;

    CClass* AssetClass=nullptr;
    switch(InAssetType)
    {
        case EEditorSelectableAssetType::Texture:
            AssetClass= RTexture2D::StaticClass();
            break;
        case EEditorSelectableAssetType::Material:
            AssetClass=RMaterial::StaticClass();
            break;
        case EEditorSelectableAssetType::StaticMesh:
            AssetClass=RStaticMesh::StaticClass();
            break;
    }

    auto AssetList=RProjectViewModel::Get()->GetSubsystem<RProjectAssetSubsystem>()
        ->GenerateAssetListByClass(AssetClass);

    int64_t LineNum=AssetList.Num();
   
    if(LineNum==0)
    {
        auto NothingText=NewWidget<RTextWidget>(this);
        NothingText->Anchor.CenterIn(this);
        NothingText->Text=U"找不到可用的资产";
        return;
    }

    
    const float LineHeight=20;
    const float LineInterval=4;
    
    for(int64_t LineIndex=0;LineIndex<LineNum;++LineIndex)
    {
        auto Line=NewWidget<RWidget>(List);
        Line->Height=LineHeight;
        Line->Anchor.HorizontalFill(List);
        Line->Y=LineIndex*(LineHeight+LineInterval)-LineInterval;

        auto Thumbnail=NewWidget<RImageWidget>(Line);
        Thumbnail->Width=LineHeight;
        Thumbnail->Height=LineHeight;
        RImageWidgetWeakPtr WeakThumbnail=Thumbnail;
        RProjectViewModel::Get()->GetSubsystem<RProjectAssetSubsystem>()
            ->AsyncLoadThumbnail(AssetList[LineIndex].Path
            ,[WeakThumbnail](TSharedObjectPtr<RTexture2D> InTexture)
            {
                if(WeakThumbnail.IsValid())
                {
                    WeakThumbnail->Image=InTexture;
                }
            }   
        );
       
        auto Name=NewWidget<RTextWidget>(Line);
        Name->Height=LineHeight;
        Name->Anchor.LeftReference=Thumbnail;
        Name->Anchor.LeftOffset=4;
        Name->Anchor.LeftReferenceEdge=EHorizontalWidgetEdge::Right;
        Name->Anchor.RightReference=Line;
        
        SPath ProjectDirectory= REditor::Get()->GetSubsystem<RProjectModel>()->GetProjectDirectory().GetValue();
        Name->Text=AssetList[LineIndex].Path.GetRelativeTo(ProjectDirectory).ToString();
       
        auto LineMouse=NewWidget<RMouseWidget>(Line);
        LineMouse->Anchor.Fill(Line);
        
        SAssetPath AssetPath=AssetList[LineIndex].ReferencePath;
        LineMouse->OnClicked.BindLambda([this,LineIndex,AssetPath](const SMouseButtonEvent&)->bool
        {
            auto Object= RAssetSubsystem::Get()->LoadAsset(AssetPath);
            OnAssetSelected.ExecuteIfBound(Object);
            return true;
        });

    }
    
}

